#include"game.h"

void Initboard(char board[ROWS][COLS], int row, int col, int set)
{
	int i = 0;
	int j = 0;
	for(i = 0; i < row; i++)
	{
		for(j = 0; j < col; j++)
		{
			board[i][j] = set;
		}
	}
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	cout<<"——————————扫雷游戏——————————————"<<endl; 
	for(j = 0; j <= col; j++)
	{
		cout<<j<<" ";
	}
	cout<<endl;
	for(i = 1; i <= row; i++)
	{
		cout<<i<<" ";
		for(j = 1; j <= col; j++)
		{
		
			cout<<board[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<"——————————扫雷游戏——————————————"<<endl; 
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
	int count = EASY_count;
	while(count)
	{
	int x = rand() % row+1;
	int y = rand() % col+1;
	 if(board[x][y] == '0')
    	{
		board[x][y] = '1';
		count--;
    	}
	}
}

 int CalculationMine(char board[ROWS][COLS], int x, int y)
{
//	int i = 0;
//	int j = 0;
//	int s = 9;
//	int sum = 0;
//	if(s>0)
//	{
//	 for(i = -1; i < 1; i++)
//	 {
//		for(j = -1; j < 1; j++)
//		{
//			 sum = board[i][j] + CalculationMine(board, x, y);
//			s--;
//		}
//	 } 
//    }
//    return sum - '0'*9;
  return (board[x - 1][y] +
    board[x - 1][y - 1] +
    board[x][y - 1] +
    board[x + 1][y - 1] +
    board[x + 1][y] +
    board[x + 1][y + 1] +
    board[x][y + 1] +
    board[x - 1][y + 1] - 8*'0');


}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	
	int x = 0;
	int y = 0;
	int win = 0;
    while(win < row*col-EASY_count)
    {
	cout<<"请输入要排查的坐标：>";
    cin>>x>>y;
	  if(x >= 1 && x <= row && y >= 1 && y <= col)
	   {
		    if(mine[x][y] == '1')
		 {
			  cout<<"很遗憾，你被炸死了。"<<endl;
		    DisplayBoard(mine, ROW, COL);
			break;
		 }
	    	else
		 { 
			  //计算出周围雷的个数
			  win++;
              int count = CalculationMine(mine, x, y);
              show[x][y] = count + '0';//转换数字字符 
              DisplayBoard(show, ROW, COL);
         
		 }
	    }
	  else
	   {
	    	cout<<"输入坐标不合法，请重新输入。"<<endl; 
	   }
	}
	if(win == row*col-EASY_count)
	{
		cout<<"恭喜你，排雷成功"<<endl; 
		DisplayBoard(mine, ROW, COL);
		
	}
}
